/*
 *所有关于cw_djlc_cwzb_view类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cw_djlc_cwzb_viewService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using System.Collections.Generic;
using VOL.Core.DBManager;
using System;

namespace VOL.Finance.Services
{
    public partial class cw_djlc_cwzb_viewService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icw_djlc_cwzb_viewRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public cw_djlc_cwzb_viewService(
            Icw_djlc_cwzb_viewRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
         /*   var marginalrate ="" ;
            var factoryprice = "";
            WebResponseContent webContent = new WebResponseContent();
            var finalprice = saveModel.MainData.Where(S => S.Key == "finalprice").Select(S => S.Value).ToList().FirstOrDefault();
            var wlzbId = saveModel.MainData.Where(S => S.Key == "cw_djlc_wlzb_id").Select(S => S.Value).First();
            var rebateconditions = saveModel.MainData.Where(S => S.Key == "rebateconditions").Select(S => S.Value).First();
            var jgwhId = saveModel.MainData.Where(S => S.Key == "cw_djlc_jgwh_id").Select(S => S.Value).First();
            if ((finalprice != null && finalprice.ToString() != "") && !StringExtension.IsNumber(finalprice.ToString(), 8, 6))
            {
                webContent.Code = "-2";
                return webContent.Error("输入不正确，请重新输入！");
            }*/
            UpdateOnExecuting = (cw_djlc_cwzb_view view, object addList, object updateList, List<object> delKeys) =>
            {
               /* var marginalrate = view.marginalrate; //边际率
                var factoryprice = view.factoryprice; //标准出厂价
                var dcjb = view.dcjb; //单车降本
                var air_bottle_testing = view.air_bottle_testing;//气瓶检测*/
                var djzbId = view.cw_djlc_djzb_id;
                var cwzbId = view.vehiclekeyword;
                string sql = "";
                //这么写要求边际率 出厂价格不能为空了
                /*if (dcjb == null || "".Equals(dcjb)) 
                {
                    sql = "update cw_djlc_djzb set [dcjb] = null where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { djzbId });
                }
                else
                {
                    sql = "update cw_djlc_djzb set [dcjb] = @dcjb where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { dcjb, djzbId });
                }
                if (marginalrate == null && "".Equals(marginalrate))
                {
                    sql = "update cw_djlc_djzb set [marginalrate] = null where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { djzbId });
                }
                else
                {
                    sql = "update cw_djlc_djzb set [marginalrate] = @marginalrate where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { marginalrate, djzbId });
                }
                if (factoryprice == null || "".Equals(factoryprice))
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = null where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql2, new { djzbId });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = @factoryprice where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql2, new { factoryprice, djzbId });
                }*/
                if (view.vehiclekeyword!=null && !"".Equals(view.vehiclekeyword))
                {
                    sql = "update cw_djlc_djzb set [cw_djlc_cwzb_id] = @cwzbId where cw_djlc_djzb_id = @djzbId";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { cwzbId, djzbId });
                }
                webResponse.Code = "-1";
                return webResponse.OK("保存成功！");
            };
            return base.Update(saveModel);
        }
    }
}
